<?php
/**
 * @package: Asalsuri
 * @subpackage: Admin Model
 * @author: Truong Kim Phung - Pham Van Diep
 * @date: January 24, 2014 - January 27, 2014
 * 
*/
class Admin_Model extends CI_Model{
	var $database;
	public function __construct() {
        parent::__construct();
		$this->database = $this->db->conn_id;
		
	}
    /**
     * @author: Truong Kim Phung
     * @description: create object admin
     * @param array $row: array admin 
     * @return object | null
    */
	private function adminToObj($row) {
		if(isset($row)) {
			$objAdmin			= new stdClass();
			$objAdmin->id 		= isset($row['adminid']) ? $row['adminid'] : 0;
			$objAdmin->username = isset($row['username']) ? $row['username'] : '';
			$objAdmin->fullname = isset($row['fullname']) ? $row['fullname'] : '';
			$objAdmin->type 	= isset($row['type']) ? $row['type'] : 0;
			$objAdmin->password	= isset($row['password']) ? $row['password'] : '';
            $objAdmin->roomid   = isset($row['roomid']) ? $row['roomid'] : 0;
			return $objAdmin;
		}
		return null;
	}
    /**
     * @author: Truong Kim Phung
     * @description: get admin info
     * @param integer $intId: ID
     * @return object | null
    */
	public function get($intId) {	
		$stmt = $this->database->prepare('CALL admins_get(?)');
		$stmt->bindParam(1, $intId, PDO::PARAM_INT, 11);
		$stmt->execute();
		return  $this->adminToObj($stmt->fetch());				
	}
    /**
     * @author: Truong Kim Phung
     * @description: get admin info
     * @param string $strUsername: username Admin
     * @return object | null
    */
	public function getInfoByUsername($strUsername) {		
		$stmt = $this->db->conn_id->prepare('call admins_get_username(?);');
		$stmt->bindParam(1, $strUsername, PDO::PARAM_STR, 30);
		$stmt->execute();
		return $this->adminToObj($stmt->fetch());
		
	}
    /**
    * @author: Pham Van Diep
    * @final: Truong Kim Phung
    * @description: get all admins
    * @param integer $intType: admin role loggin
    * @param integer $intOffset: offset start paging
    * @param integer $intLimit: display row on paging
    * @param integer $intTotal: assign found rows 
    * @param array
    */
    public function getAll($intType, $intOffset, $intLimit, &$intTotal=0) {
		$stmt = $this->database->prepare('CALL admins_getall(?,?,?)');
		$stmt->bindParam(1, $intType,     PDO::PARAM_INT, 4);
		$stmt->bindParam(2, $intOffset,   PDO::PARAM_INT, 11);
		$stmt->bindParam(3, $intLimit,    PDO::PARAM_INT, 6);		
		$stmt->execute();
		$result = $stmt->fetchAll();        
		$intTotal = isset($result[0]['foundRows']) ? $result[0]['foundRows'] : 0;
		return isset($result) ? $result : null;
	}
	/**
     * @author: Pham Van Diep
     * @final: Truong Kim Phung 
	 * @description: create new account 
	 * @param interger $intLoginId: id admin loggin (as root id width insert permission)
	 * @param string $strUsername:
	 * @param string $strPassword: encrypt md5
	 * @param string $strFullname:
	 * @param integer $intType: role 
     * @param integer $intRoomId: working room
	 * @return integer
     *    0: INSERT SUCCESS
     *    1: ERROR - USERNAME EXISTS
     *    2: ERROR - NOT PERMISSION ADD
     *      	
	 */
	public function add($intLoginId, $strUsername, $strPassword, $strFullname, $intType=0, $intRoomId=0) {
		$stmt = $this->database->prepare('CALL admins_add(?,?,?,?,?,?)');
		$stmt->bindParam(1, $intLoginId,  PDO::PARAM_INT, 11);
		$stmt->bindParam(2, $strUsername, PDO::PARAM_STR, 30);
		$stmt->bindParam(3, $strPassword, PDO::PARAM_STR, 150);
		$stmt->bindParam(4, $strFullname, PDO::PARAM_STR, 150);
		$stmt->bindParam(5, $intType,     PDO::PARAM_INT, 4);		
		$stmt->bindParam(6, $intRoomId,   PDO::PARAM_INT, 4);		
		$stmt->execute();	
        $result = $stmt->fetch();
        return isset($result['errCode']) ? $result['errCode'] : -1;	
	}
	/**
     * @author: Pham Van Diep
     * @final: Truong Kim Phung 
	 * @description: update account 
	 * @param interger $intLoginId: id admin loggin (as root id width insert permission)
     * @param integer $intAdminId: ID
	 * @param string $strUsername: Username
	 * @param string $strPassword: encrypt md5
	 * @param string $strFullname:
	 * @param integer $intType: role 
     * @param integer $intRoomId: working room
	 * @return integer
     *    0: INSERT SUCCESS
     *    1: ERROR - NOT PERMISSION UPDATE
     *      	
	 */
	public function update($intLoginId, $intAdminId, $strUsername, $strPassword, $strFullname, $intType, $intRoomId=0) {
		$stmt = $this->database->prepare('CALL admins_update(?,?,?,?,?,
                                                             ?,?)');
		$stmt->bindParam(1, $intLoginId,  PDO::PARAM_INT, 11);
		$stmt->bindParam(2, $intAdminId,  PDO::PARAM_INT, 11);
		$stmt->bindParam(3, $strUsername, PDO::PARAM_STR, 30);
		$stmt->bindParam(4, $strPassword, PDO::PARAM_STR, 64);
		$stmt->bindParam(5, $strFullname, PDO::PARAM_STR, 150);
		$stmt->bindParam(6, $intType,     PDO::PARAM_INT, 4);		
		$stmt->bindParam(7, $intRoomId,   PDO::PARAM_INT, 4);		
		$stmt->execute();
		$result = $stmt->fetch();
		return isset($result['errCode']) ? $result['errCode'] : -1;	
	}
    /**
     * @author: Pham Van Diep
     * @final: Truong Kim Phung 
	 * @description: get all employee in room is actived 
	 * @param interger $intType: role login
     * @param integer $intRoomId: room ID. -1: ADMIN | ROOT
	 * @return array | null
     *      	
	 */
	public function getEmployee($intType,$intRoomId) {
		$stmt = $this->database->prepare('CALL admins_getall_com(?,?)');
		$stmt->bindParam(1, $intType,     PDO::PARAM_INT, 4);
        $stmt->bindParam(2, $intRoomId,   PDO::PARAM_INT, 11);
		$stmt->execute();
		$arrResult = $stmt->fetchAll();
        return isset($arrResult) ? $arrResult : null;
	}
    /**
     * @author: Truong Kim Phung
	 * @description: change password  
	 * @param interger $intLoginId: id admin loggin (as root id width insert permission)
     * @param integer $intAdminId: ID
	 * @param string $strUsername: Username
	 * @param string $strPassword: encrypt md5
	 * @return integer
     *    0: INSERT SUCCESS
     *    1: ERROR - NOT PERMISSION UPDATE
     *      	
	 */
	public function updatePassword($intLoginId, $intAdminId, $strUsername, $strPassword) {
        $stmt = $this->database->prepare('CALL admins_update_password(?,?,?,?)');
		$stmt->bindParam(1, $intLoginId,    PDO::PARAM_INT, 11);
        $stmt->bindParam(2, $intAdminId,    PDO::PARAM_INT, 11);
        $stmt->bindParam(3, $strUsername,   PDO::PARAM_STR, 30);
        $stmt->bindParam(4, $strPassword,   PDO::PARAM_STR, 64);
		$stmt->execute();
        $result = $stmt->fetch(); 
		return isset($result['errCode']) ? $result['errCode'] : -1;
	}
	public function updateStatus() {
	
	}
	/**
	*	@description: get Error code from query result
	*	@param: mixed query result
	*	@return: int
	*/
	public function getErrorCode ( $tableData) {
		return isset($tableData['errCode'])?intval($tableData['errCode']):0;
	}	
	
}